/**
 * Copyright (C) 2009+ Spark Zheng
 *
 * @file        src/protocol.h 
 * @date        2010-04-05
 * @brief	server_framework
 * @see         dserver
 * @history
 */

#ifndef _DSERVER_PROTOCOL_H
#define _DSERVER_PROTOCOL_H

#ifdef __cplusplus
extern "C" {
#endif

#include <dlib/global.h>
#include <dlib/list.h>
#include <dlib/lock.h>
#include <dlib/bit.h>

#include "dserver.h"
#include "dserver_log.h"
#include "dserver_errno.h"
#include "prot_types.h"

#define P_CUSTOM		"custom"
#define P_DEF_CUSTOM		0
#define P_HOST			"host"
#define P_DEF_HOST		"127.0.0.1"
#define P_PORT			"port"
#define P_DEF_PORT		10011
#define P_TYPE			"type"
#define P_DEF_TYPE		"tcp"
#define P_TIMEOUT		"timeout"
#define P_DEF_TIMEOUT		30
#define P_PRIORITY		"priority"
#define P_DEF_PRIORITY		2
#define P_TCP_NODELAY		"tcp_nodelay"
#define P_DEF_TCP_NODELAY	0
#define P_TCP_KEEPALIVE		"tcp_keepalive"
#define P_DEF_TCP_KEEPALIVE	0
#define P_MAX_RX_BUF		"max_rx_buf"
#define P_DEF_MAX_RX_BUF	0
#define P_MAX_TX_BUF		"max_tx_buf"
#define P_DEF_MAX_TX_BUF	0

#ifndef TCP_STRING
#define TCP_STRING		"tcp"
#endif
#ifndef UDP_STRING
#define UDP_STRING		"udp"
#endif
#ifndef SCTP_S_STRING
#define SCTP_S_STRING		"sctp_stream"
#endif
#ifndef SCTP_D_STRING
#define SCTP_D_STRING		"sctp_dgram"
#endif

typedef struct dserver_protocol	dserver_prot_node_t;

struct dserver_protocol {
	prot_node_t	p_prot;		/** protocol node */
	void		*p_handler;	/** protocol lib.so handler */
	list_node_t	p_node;		/** protocol list_node */
};

/**
 * protocol_context for all loaded protocols
 */
struct protocol_context {
	dlib_atomic_t	p_nr;		/** protocols number */
	prot_node_t	*p_monitor;	/** the monitor protocol node */
	list_head_t	p_head;		/** protocols list head */
	dlib_lock_t	p_lock;		/** monitor may workes in thread */
};

/**
 * protocol_init
 * @brief construct protocol module
 * @param dc: main context struct
 */
extern int protocol_init(dserver_t *dc);

/**
 * protocol_exit
 * @brief destruct protocol module
 * @param dc: main context struct
 */
extern void protocol_exit(dserver_t *dc);

#ifdef __cplusplus
}
#endif
#endif
